package com.eastfair.questionnaire.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.echo.core.EchoService;
import com.eastfair.questionnaire.dto.*;
import com.eastfair.questionnaire.entity.Answer;
import com.eastfair.questionnaire.service.AnswerService;
import com.eastfair.questionnaire.service.RelationTagService;
import com.eastfair.questionnaire.vo.AnswerVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 标签映射
 * </p>
 *
 * @author yuwx
 * @date 2021-08-18
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/relationTag")
@Api(value = "relationTag", tags = "标签映射")
public class RelationTagController  {

    private RelationTagService relationTagService;
    private EchoService echoService;
    @Autowired
    public RelationTagController(RelationTagService relationTagService, EchoService echoService) {
        this.relationTagService = relationTagService;
        this.echoService = echoService;
    }


    @ApiOperation(value = "保存答案、标签映射关系", notes = "保存答案、标签映射关系")
    @PostMapping("/save")
    public R<Boolean> save(
            @Validated({RelationTagDTO.validateBase.class, RelationTagDTO.validateSave.class})
            @RequestBody RelationTagDTO relationTag) {
        Long projectId = ContextUtil.getProjectId();
        relationTagService.saveTagAnswerRelation(projectId, relationTag.getSystemSourceEnum(), relationTag.getAnswerId(), relationTag.getTagIdList());
        return R.successDef();
    }

    @ApiOperation(value = "答案转标签", notes = "答案转标签")
    @PostMapping("/convertToTag")
    public R<List<Long>> convertToTag(
            @Validated({RelationTagDTO.validateBase.class, RelationTagDTO.validateGet.class})
            @RequestBody RelationTagDTO relationTag) {
        Long projectId = ContextUtil.getProjectId();
        List<Long> tagIdList = relationTagService.convertAnswerToTag(projectId, relationTag.getSystemSourceEnum(), relationTag.getAnswerIdList());
        return R.success(tagIdList);
    }

    @ApiOperation(value = "答案转标签,保持分组结构", notes = "答案转标签,保持分组结构")
    @PostMapping("/convertToTagGroup")
    public R<List<List<Long>>> convertToTagGroup(
            @Validated({RelationTagDTO.validateBase.class, RelationTagDTO.validateGet.class})
            @RequestBody RelationTagDTO relationTag) {
        Long projectId = ContextUtil.getProjectId();
        List<List<Long>> answerIdGroupList = relationTag.getAnswerIdGroupList();
        if (CollectionUtil.isEmpty(answerIdGroupList)) {
            return R.success(Collections.EMPTY_LIST);
        }
        answerIdGroupList = answerIdGroupList.stream().filter(answerIdList -> !CollectionUtil.isEmpty(answerIdList)).collect(Collectors.toList());

        List<List<Long>> tagIdGroupList =
                answerIdGroupList.stream().map(answerIdList ->
                      relationTagService.convertAnswerToTag(projectId, relationTag.getSystemSourceEnum(), answerIdList)).collect(Collectors.toList());
        return R.success(tagIdGroupList);
    }
}
